/**
*字符串相关操作
*author diqye
*/
(function(root,factory){
	if (typeof exports === 'object') {
        // Node. 
        module.exports = factory.call(root,
        		require("./diqye-util"),
        		require("./syntaxs/borderSyntax"),
        		require("./methods/fillMethod")
        );
    } else if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(["diqye/diqye-util","diqye/syntaxs/borderSyntax","diqye/methods/fillMethod"],function(a,b,c){ 
        	return factory.call(root,a,b,c);
         });
    } else {
        // Browser globals (root is window)
        window.strs = factory.call(root,utilfn,borderSyntax,fillMethod);
    }
}(this,function(util,borderSynatax,fillMethod){
	
	function diqye(str){
		var r =diqye.string("string_tmpl",{str:str});
		r.fill = function(data){
			return this.replace(data);
		} 
		return r;
	}

	//字符串相关操作基类
	diqye.string = util.decorator("string",{
		init:function(options){
			this.str = options.str||"";
		},
		toInt:function(){
			return parseInt(this.str);
		},
		toFloat:function(){
			return parseFloat(this.str);
		}
	});

	//简单模版
	diqye.string('string_tmpl',function(parent){
		return {
				init:function(options){
					parent.init.call(this,options);
					this.syntax = options.syntax||borderSynatax;
					this.method = options.method||fillMethod;
				},
				setSyntax:function(syntax){
					this.syntax = syntax;
					return this;
				},
				setMethod:function(method){
					this.method = method;
					return this;
				},
				replace:function(options){
					var sbuf = [],
						str1,
						start = 0,
						matchingIndex = {start:0,end:0};
					while(this.syntax.call(this.str,matchingIndex)){
						sbuf.push(this.str.substring(start,matchingIndex.start));
						str1 = this.str.substring(matchingIndex.start,matchingIndex.end);
						str1 = this.method.call(this.str,str1,options);
						sbuf.push(str1);
						start = matchingIndex.end;
						matchingIndex.start = start;
					}
					sbuf.push(this.str.substr(start));
					return sbuf.join("");
				}
		}
	});
	return diqye;
}));